# -*- coding: utf-8 -*-
# requests
# urllib

import requests
import bs4
from urllib.request import urlretrieve, urlopen


def requests_test():
    req = requests.get('https://www.bilibili.com/')
    print(req)
    print(req.headers)
    print(req.text)  # str
    print(req.content)  # bytes
    for k, v in req.cookies.iteritems():
        print(k, v)
    print(req.url)

    soup = bs4.BeautifulSoup(req.text, features='lxml')
    print(soup.head)


def urllib_test():
    url = r'http://nau.edu.cn/_upload/tpl/01/f9/505/template505/images/logo.png'
    urlretrieve(url, 'logo.png')

    response = urlopen('http://www.baidu.com')
    print(response.read().decode('utf-8'))


def requests_test2():
    payload = {'key1': 'value1', 'key2': 'value2'}
    r = requests.get('https://httpbin.org/get', params=payload)
    print(r.url)


def requests_test3():
    URL = 'https://www.alljobs.co.il/SearchResultsGuest.aspx'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:80.0) Gecko/20100101   Firefox/80.0',
        'Cookie': 'some Cookie string'
    }

    def get_html(url, params=None):
        r = requests.get(url, headers=headers, params=params)
        return r.text

    params = {'page': 1, 'position': '765', 'type': '', 'city': '', 'region': ''}
    html = get_html(URL, params=params)
    print(html)


if __name__ == '__main__':
    requests_test()
    # urllib_test()
